/**
* Package containing all of the utility classes for dealing with components' descriptors (component.xml,as defined by component.xsd)
*/
jsx3.lang.Package.definePackage("ux.core.util.component", function(ux) {
});

/**
* Internal helper class with a set of utilities for creating <code>ComponentConfig</code> objects from component.xml content, and resolving URIs for their resources.
*/
jsx3.lang.Class.defineClass("ux.core.util.component.ComponentUtils",
        jsx3.lang.Object,
        null,
        function(utils) {

            /**
            * Internal function to create a <code>ComponentConfig</code> object from xml content representing a component's
            * component descriptor (component.xml).
            *
            * @param str {String} either the XML content itself, or an absolute URI to the component.xml file
            *
            * @return {ux.core.util.component.ComponentConfig} the <code>CoomponentConfig</code> object wrapping the component's
            *         descriptor information
            *
            * @throws {ux.core.exception.InvalidComponentSchemaException if the <code>component.xml</code> document is not valid
            * @throws {jsx3.lang.Exception} if an unknown error occurs while creating the <code>ux.core.util.component.ComponentConfig</code> object
            *
            * @private
            * @jsxobf-clobber
            */
            utils.createConfig = function(str) {
                    var doc = new jsx3.xml.Document();
                    var error = doc.getError();
                    if(error && (error.code != 0)) {
                        throw new jsx3.lang.Exception("Error creating XML document: Error code " + error.code + ". Error message is as follows: " + error.description);
                    }
                    doc.loadXML(str);
                    error = doc.getError();
                    if(error.code != 0) {
                        var message = "" + error.description;
                        var code = error.code;
//                    }
//                    if(!doc.getRootNode()) {
                        doc.load(str);
                        error = doc.getError();
                        if(error.code != 0) {
                            throw new jsx3.lang.Exception("Error creating XML document - \nFor loadXML: error code is " + code + " and message is: " + message + " \nFor load: error code is " + error.code + " and message is " + error.description);
                        }
                    }
                try {
                    return new ux.core.util.component.ComponentConfig(doc);
                } catch(ex) {
                    if(ex instanceof ux.core.exception.InvalidComponentSchemaException) {
                        throw new jsx3.lang.Exception("Cold not create config object from src " + str, ex);
                    }
                    else {
                        throw new jsx3.lang.Exception("Unknown exception creating component ux.core.util.component.ComponentConfig object", jsx3.NativeError.wrap(ex));
                    }
                }
            }

            /**
            * Internal function to resolve a URI for a component's resource using it's plugin package's namespace.
            *
            * @param namespace {String} the plugin package namespace of this resource's plugin package
            * @param relPath {String} the path to the resource, relative to the plugin's context root
            *
            * @return {String} the resolved URI, or <code>null</code> if no contextRoot can be found for the given namespace
            *
            * @throws {ux.core.exception.InvalidNamespaceException} if <code>namespace</code> is invalid
            * @throws {jsx3.lang.Exception} if an unknown error occurs
            *
            * @private
            * @jsxobf-clobber
            */
            utils.resolveURI = function(namespace, relPath) {
                var ret = null;
                try {
                    ret = ux.core.System.getContextRootForNamespace(namespace) + "/";
                    if(ret) {
                        if(ux.core.System.isFileSystem()) {
                            ret = ux.core.System.getGBaseURI() + "/" + ret + ((relPath.indexOf("/") == 0) ? relPath.substring(1) : relPath);
                        }
                        else {
                            ret = "/" + ret + ((relPath.indexOf("/") == 0) ? relPath.substring(1) : relPath);
                        }
                    }
                }
                catch(ex) {
                    throw jsx3.NativeError.wrap(ex);
                }
                return ret;
            }

            /**
            * Internal function to resolve a URI for a component's resource using it's plugin package's contextRoot.
            *
            * @param contextRoot {String} the contextRoot of this resource's plugin package
            * @param relPath {String} the path to the resource, relative to the plugin's context root
            *
            * @return {String} the resolved URI, or <code>null</code> if no contextRoot is given
            *
            * @throws {jsx3.lang.Exception} if an unknown error occurs
            *
            * @private
            * @jsxobf-clobber
            */
            utils.resolveURIFromContextRoot = function(contextRoot, relPath) {
                var ret = null;
                try {
                    ret = contextRoot + "/";
                    if(ret) {
                        if(ux.core.System.isFileSystem()) {
                            ret = ux.core.System.getGBaseURI() + "/" + ret + ((relPath.indexOf("/") == 0) ? relPath.substring(1) : relPath);
                        }
                        else {
                        	var realPath = relPath;
                        	if(realPath.indexOf("/") == 0){
                        		 realPath = realPath.substring(1);
                        	}
                        	if(realPath.indexOf("JSXAPPS/administrator") == 0) {
                        		 realPath = realPath.substring(21);
                          }
                            ret = "/" + ret + realPath;
                        }
                    }
                }
                catch(ex) {
                    throw jsx3.NativeError.wrap(ex);
                }
                return ret;
            }
        }
);
